<template>
  <a-card :loading="loading" :bordered="false">
    <!-- 业务表单 -->
    <a-form-model ref="form" :model="formData" :rules="formRules" :form="form" style="margin-top: 8px">
      <!-- 队伍名称 -->
      <a-form-model-item prop="name" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="队伍名称">
        <a-input placeholder="请输入志愿者队伍名称"
        v-model="formData.name"
        :disabled="!editable || editableFiexd['name']" />
      </a-form-model-item>

      <!-- 所属负责人 -->
      <a-form-model-item prop="directorId" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="负责人">
        <x-select-user-by-dep
          v-model="formData.directorId"
          :multi="false"
          :disabled="isFlow||!editable || editableFiexd['directorId']"
          @selected="setUserName"
        ></x-select-user-by-dep>
      </a-form-model-item>

      <!-- 所属部门 -->
      <a-form-model-item
        :labelCol="{ span: 5 }"
        :wrapperCol="{ span: 15 }"
        label="所属部门"
        prop="sysOrgCode"
      >
        <j-select-depart
          v-model="formData.sysOrgCode"
        :disabled="!editable||editableFiexd['sysOrgCode']" @selected="setDepart" />
      </a-form-model-item>

      <!-- 服务对象 -->
      <a-form-model-item prop="customer" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="服务对象">
        <a-input placeholder="请输入服务对象" 
         v-model="formData.customer"
        :disabled="!editable || editableFiexd['customer']" />
      </a-form-model-item>

      <!-- 上传图片 -->
      <a-form-model-item prop="pics" label="图片" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-image-upload text="上传图片" v-model="formData.pics" bizPath="volunteerTeam" :disabled="!editable" :isMultiple="true"></j-image-upload>
      </a-form-model-item>

      <!-- 上传附件 -->
      <a-form-model-item prop="enclosures" label="附件" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-upload 
        :disabled="!editable"
        v-model="formData.enclosures" text="上传文件" bizPath="volunteerTeam"></j-upload>
      </a-form-model-item>

      <!-- 指导老师 -->
      <a-form-model-item prop="instructorId" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="指导老师">
        <x-select-user-by-dep
          v-model="formData.instructorId"
          :disabled="!editable || editableFiexd['instructorId']"
          :multi="false" @selected="setInstructor"></x-select-user-by-dep>
      </a-form-model-item>
      <!-- 成立时间 -->
      <a-form-model-item prop="buildTime" v-if="showBuildTime" label="成立时间" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }">
        <j-date
          placeholder="请选择日期"
          :disabled="!editable"
          v-model="formData.buildTime"
          :trigger-change="true"
          style="width: 100%"
        />
      </a-form-model-item>
      <!-- 解散时间 -->
      <a-form-model-item prop="dismissTime" v-if="canDismiss" label="解散时间" 
        :labelCol="{ span: 5 }"
        :wrapperCol="{ span: 15 }">

        <j-date
          placeholder="请选择解散时间！"
          :disabled="!editable"
          v-model="formData.dismissTime"
          :trigger-change="true"
          style="width: 100%"
        />
      </a-form-model-item>
    </a-form-model>
  </a-card>
</template>

<script>

import JDate from '@/components/jeecg/JDate.vue'
import { httpAction, getAction } from '@/api/manage'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JUpload from '@/components/jeecg/JUpload'
import { getVolunteerTeamById } from '@/api/api'
import pick from 'lodash.pick'
export default {
  name: 'VolunteerTeam',
  components: {
    JDate,
    JImageUpload,
    JUpload,
    JSelectDepart,
    JSelectMultiUser,
    XSelectUserByDep
  },
  model: {
    prop: 'data',
    event: 'onchange'
  },
  props: {
    isFlow:Boolean,
    dataId: String,
    data: {
      type: Object,
      default: () => {
        return {}
      }
    },
    showBuildTime: Boolean,
    canDismiss: {
      type: Boolean,
      default: false
    },
    editable: {
      type: Boolean,
      default: true
    },
    editableFiexd: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading: true,
      userDisabled:false,
      url: {
        add: '/volunteerTeam/add',
        edit: '/volunteerTeam/edit',
        queryById: '/volunteerTeam/queryById'
      },
      imageList:[],
      fileList:[],
      form: this.$form.createForm(this),
      formData: {
        createTime: '',
        creatorId: '',
        creatorName: '',
        id: '',
        customer: '',
        directorId: '',
        director: '',
        enclosures: ''
      },
      formRules:{
        name:[{ required: true, message: '志愿者队伍名称不能为空！' }],
        directorId:[{ required: true, message: '负责人不能为空！' }],
        sysOrgCode:[{ required: true,message: '所属部门不能为空！' }],
        customer:[{ required: true, message: '服务对象不能为空！' }],
        instructorId:[{ required: true, message: '请选择指导老师！' }],
        buildTime:[{ required: true, message: '请选择成立时间！' }],
      }
    }
  },
  beforeMount() {
    if (this.dataId) {
      this.queryData(this.dataId)
    }else{
      this.loading = false
    }
  },
  methods: {
    add(data) {
      this.userDisabled = true
      this.open(data)
    },
    apply(data) {
      this.userDisabled = true
      this.open(Object.assign({},data,{
        workflowModel: {
          operation: 'start'
        }
      }))
    },
    edit(record) {
      this.userDisabled = true
      this.open(record)
    },
    open(record) {
      this.form.resetFields()
      this.loading = false
      if (record.id) {
        this.queryData(record.id).then(()=>{
          if(record.workflowModel) Object.assign(this.formData,{workflowModel:record.workflowModel})
        })
      }else{
        this.formData = Object.assign({}, record)
      }
    },
    setDepart(data) {
      this.formData.departName = data.name
    },
    setUserName(data) {
      this.formData.director = data.name
    },
    setInstructor(data){
      this.formData.instructor = data.name
    },
    //查询表单数据
    queryData(id) {
      this.loading = true
      let params = { id }
      return getAction(this.url.queryById, params).then(res => {
        if (res.success) {
          this.formData = Object.assign({}, this.formData, res.result)
        }
        return res
      }).finally(()=>{
        this.loading = false
      })
    },
    submitForm() {
      const that = this
      return new Promise((resolve, reject) => {
        // 触发表单验证
        that.$refs.form.validate((valid, fields) => {
          if (valid) {
            let httpurl = ''
            let method = ''
            if (!that.formData.id) {
              httpurl += that.url.add
              method = 'post'
            } else {
              httpurl += that.url.add
              method = 'post'
            }
            let formData = Object.assign({},that.formData)
            // if(that.fileList.length){
            //   formData.enclosures = that.fileList
            // }else{
            //   formData.enclosures = ''
            // }
            // if(that.imageList.length){
            //   formData.pics = that.imageList
            // }else{
            //   formData.pics = ''
            // }

            httpAction(httpurl, formData, method)
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                  resolve(res)
                } else {
                  that.$message.warning(res.message)
                  reject(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          }else{
            that.$message.warning('请检查表单信息')
          }
        })
      })
    },
  },
  watch: {
    dataId(nv, on) {
      if (nv&&nv!=ov) {
        this.formData = {}
        this.queryData(nv)
      }
    },
    data: {
      handler(nv, ov) {
        this.formData = nv
      },
      immediate: true,
      deep: true
    },
    formData: {
      handler(nv, ov) {
        this.$emit('onchange', nv)
      },
      immediate: true,
      deep: true
    }
  }
}
</script>

<style></style>
